package com.deep.search.controller;

import com.deep.common.model.dto.es.SkuEsModel;
import com.deep.common.model.enume.BizCodeEnum;
import com.deep.common.utils.R;
import com.deep.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * Elasticsearch保存控制器
 *
 * @author Deep
 * @date 2022/1/24
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class SaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     * Fewer arguments provided (0) than placeholders specified (1)
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean status;
        try {
            status = productSaveService.productUp(skuEsModels);
        } catch (IOException e) {
            log.error("SaveController商品上架错误:{}", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return status ? R.ok() : R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
